package socket;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ChatServer {

    List<PrintWriter> escritores = new ArrayList<PrintWriter>();

    public ChatServer() {
        ServerSocket server;
       // Scanner leitor;


        try {
            server = new ServerSocket(5000);
            while (true) {
                Socket s = server.accept();
                new Thread(new EscutaCliente(s)).start();
                PrintWriter p = new PrintWriter(s.getOutputStream());
                escritores.add(p);
                //leitor = new Scanner(s.getInputStream());
                //System.out.println(leitor.nextLine());

            }
        } catch (IOException iOException) {
        }

    }

    private void encaminharParaTodos(String texto) {
        for (PrintWriter w : escritores) {
            try {
                w.println(texto);
                w.flush();
            } catch (Exception e) {
            }
        }
    }

    private class EscutaCliente implements Runnable {

        Scanner leitor;

        public EscutaCliente(Socket socket) {
            try {
                leitor = new Scanner(socket.getInputStream());
            } catch (IOException ex) {
            }

        }

        @Override
        public void run() {
            try {
                String texto;
                while ((texto = leitor.nextLine()) != null) {
                    System.out.println(texto);
                    encaminharParaTodos(texto);
                }
            } catch (Exception e) {
            }
        }
    }

    public static void main(String[] args) {
        new ChatServer();
    }
}
